package cn.epsilon3.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.epsilon3.annotation.E3Log;
import cn.epsilon3.annotation.E3Service;
import cn.epsilon3.common.Result;
import cn.epsilon3.constant.ResultEnum;
import cn.epsilon3.domain.e3.dto.add.E3ResourceAdd;
import cn.epsilon3.domain.e3.dto.query.E3ResourceQuery;
import cn.epsilon3.domain.e3.dto.update.E3ResourceUpdate;
import cn.epsilon3.domain.e3.vo.E3ResourceVO;
import cn.epsilon3.utils.SecurityUtils;
import cn.epsilon3.service.E3ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 菜单资源(E3ResourcePO)表控制层
 *
 * @author epsilon3
 * @since 2023-11-12 21:49:55
 */
@Api(
        tags = {"E3-资源配置管理器v1.0.2.pre-alpha"}
)
@E3Service(
        path = "/resource"
)
public class E3ResourceController {
    /**
     * 服务对象
     */
    @Resource
    private E3ResourceService resourceService;

    @ApiOperation("根据角色加载资源树")
    @PostMapping("queryResourceTree")
    public Result<List<E3ResourceVO>> queryResourceTree() {
        String roleId = SecurityUtils.currentAccount().getRole().getRoleId();
        List<E3ResourceVO> queryRoleResource = resourceService.queryRoleResource(roleId);
        return new Result<>(ResultEnum.SUCCESS,queryRoleResource);
    }

    @SaCheckPermission(value = "system:resource:list")
    @ApiOperation("条件查询资源")
    @PostMapping("list")
    public Result<List<E3ResourceVO>> list(E3ResourceQuery resourceQuery) {
        List<E3ResourceVO> queryRoleResource = resourceService.list(resourceQuery);
        return new Result<>(ResultEnum.SUCCESS,queryRoleResource);
    }

    @SaCheckPermission(value = "system:resource:add")
    @ApiOperation("新增资源")
    @PutMapping("add")
    @E3Log
    public Result<String> add(@Valid E3ResourceAdd resourceAdd) {
        resourceService.add(resourceAdd);
        return new Result<>(ResultEnum.SUCCESS,"新增资源成功");
    }

    @SaCheckPermission(value = "system:resource:update")
    @ApiOperation("修改资源")
    @PatchMapping("update")
    @E3Log
    public Result<String> update(@Valid E3ResourceUpdate resourceUpdate) {
        resourceService.update(resourceUpdate);
        return new Result<>(ResultEnum.SUCCESS,"修改资源成功");
    }

    @SaCheckPermission(value = "system:resource:delete")
    @ApiOperation("删除资源")
    @DeleteMapping("delete")
    @E3Log
    public Result<String> delete(@RequestParam("resourceId") String resourceId) {
        resourceService.delete(resourceId);
        return new Result<>(ResultEnum.SUCCESS,"删除资源成功");
    }
}

